<template>
  <div class="swiper">
    <div class="right">
      <img src="http://jindu.dlztc.com/img/zt.17aed02d.jpg" />
    </div>
    <!-- sWpier轮播图 -->
    <div class="swiper-container" ref="cur">
      <div class="swiper-wrapper">
        <div
          class="swiper-slide"
          v-for="(item, index) in swiperList"
          :key="index"
        >
          <img :src="item.url" style="width: 2rem; height: 1rem" />
        </div>
      </div>
      <div class="swiper-button-next"></div>
      <div class="swiper-button-prev"></div>
    </div>
  </div>
</template>

<script>
// 引入swiper
import Swiper from "swiper";
export default {
  name: "Swiper",
  data() {
    return {
      swiperList: [],
    };
  },
  mounted() {
    this.getNavigation();
  },
  methods: {
    async getNavigation() {
      let result = await this.$API.receptionist.reqNavigation();
      if (result.status == 200) {
        this.swiperList = result.data.map((item) => {
          return {
            url: item.image,
          };
        });
      }
    },
  },
  watch: {
    swiperList: {
      // 立即监听：不管数据有没有变化，直接就执行一次
      immediate: true,
      handler(newValue, oldValue) {
        this.$nextTick(() => {
          var mySwiper = new Swiper(this.$refs.cur, {
            // 如果需要前进后退按钮
            loop: true, // 循环模式选项
            speed: 200, //速度
            freeMode: true, //根据惯性滑动可能不止一格且不会贴合
            autoplay: true, //自动切换
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
            // 显示几个图片
            slidesPerView: 6,
            slidesPerGroup: 1,
          });
        });
      },
    },
  },
};
</script>

<style lang="scss" scoped>
.swiper {
  width: 100%;
  height: 100px;
  display: flex;
  justify-content: space-evenly;
  margin-top: 15px;
  .right {
    width: 1rem;
    margin-left: 10px;
    img {
      width: 1.2rem;
    }
  }
  .swiper-container {
    width: 90%;
    box-sizing: border-box;
    padding: 0 12px;
    margin: 0;

    .swiper-slide {
      width: 1rem;
      height: 2rem;

      img {
        width: 1rem;
        height: 2rem;
        border: 1px solid #ccc;
        padding: 2px;
        width: 50px;
        height: 50px;
        display: block;
      }
    }

    .swiper-button-next {
      left: auto;
      right: 0;
    }

    .swiper-button-prev {
      left: 0;
      right: auto;
    }

    .swiper-button-next,
    .swiper-button-prev {
      box-sizing: border-box;
      width: 40px;
      height: 1.3rem;
      background: white;
      top: 0;
      margin-top: 0;
      &::after {
        font-size: 40px;
      }
    }
  }
}
</style>
